package com.itheima.tliassystem.controller;



import com.itheima.tliassystem.service.EmpService;
import entity.Emp;
import entity.EmpQuery;
import entity.PageBen;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
@ServletComponentScan
@RequestMapping("/emps")
@RestController
public class EmpController {
    @Autowired
    EmpService empService;
    @GetMapping
    public Result page(EmpQuery empQuery, HttpServletRequest request){
//        String token = request.getHeader("token");
//        if (token == null){
//            return Result.error("NOT_LOGIN");
//
//        }
//        try {
//            JwtUtils.parseJWT(token);
//        } catch (Exception e) {
//            return Result.error("NOT_LOGIN");
//        }


        PageBen pageBen = empService.pageEmp(empQuery);
       return Result.success(pageBen);
    }
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids , HttpServletRequest request){
        empService.deleteById(ids);
        return Result.success();
    }
    @PostMapping
    public Result add(@RequestBody Emp emp, HttpServletRequest request){
        empService.add(emp);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id ,HttpServletRequest request){
        Emp emp = empService.selectById(id);
        return Result.success(emp);
    }
    @PutMapping
    public Result update(@RequestBody Emp emp,HttpServletRequest request){
        empService.update(emp);
        return Result.success();
    }
}
